<template>
  <div class="changePassword-background">
    <div class="changePassword">
      <div class="changePassword-title">更换手机号码</div>
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item label="原手机号码" prop="oldMobile">
          <el-input v-model="form.oldMobile" :disabled="true" />
        </el-form-item>
        <el-form-item label="新手机号码" prop="newMobile">
          <el-input v-model="form.newMobile" />
        </el-form-item>
        <el-form-item label="新手机验证码" prop="code">
          <el-input v-model="form.code" />
          <el-button class="code2" type="primary" :disabled="isDisabled" @click="getCode">{{ buttonText }}</el-button>
        </el-form-item>
        <el-form-item style="margin: 50px 0">
          <el-button type="primary" @click="onSubmit('form')">确定</el-button>
          <el-button @click="onCancel()">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getUsername } from '@/utils/auth'
import { updateMobile, code } from '@/api/user'
export default {
  name: 'ChangeMobile',
  data() {
    const validateMobile = (rule, value, callback) => {
      const regMobile = /^1[3|4|5|6|7|8|9][0-9]{9}$/ // /^1(3\d|4[5-8]|5[0-35-9]|6[567]|7[01345-8]|8\d|9[025-9])\d{8}$/
      if (!value) {
        return callback(new Error('请输入手机号码'))
      } else if (value.length < 11) {
        callback(new Error('请输入手机号码'))
      } else if (!regMobile.test(value)) {
        callback(new Error('请输入正确手机号'))
      } else {
        callback()
      }
    }
    return {
      buttonText: '发送验证码',
      isDisabled: false, // 是否禁止点击发送验证码按钮
      form: {
        oldMobile: getUsername(),
        newMobile: '',
        code: ''
      },
      rules: {
        newMobile: [
          { required: true, validator: validateMobile, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请填写验证码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getCode() {
      const that = this
      const sendData = {
        username: that.from.newMobile
      }
      code(sendData).then(response => {
        if (response.result === 0) {
          this.$message({
            message: '发送失败', type: 'error'
          })
        }
        if (response.result === 2) {
          this.$message({
            message: '手机已注册', type: 'error'
          })
        }
        if (response.result === 1) {
          const username = this.from.newMobile
          this.$message({
            message: '已发送', type: 'success'
          })
          if (this.checkMobile(username)) {
            let time = 60
            this.buttonText = '已发送'
            this.isDisabled = true
            if (this.flag) {
              this.flag = false
              const timer = setInterval(() => {
                time--
                this.buttonText = time + ' 秒'
                if (time === 0) {
                  clearInterval(timer)
                  this.buttonText = '重新获取'
                  this.isDisabled = false
                  this.flag = true
                }
              }, 1000)
            }
          }
        }
      })
    },
    // 验证手机号
    checkMobile(str) {
      const re = /^1(3\d|4[5-8]|5[0-35-9]|6[567]|7[01345-8]|8\d|9[025-9])\d{8}$/
      if (re.test(str)) {
        return true
      } else {
        return false
      }
    },
    onSubmit(form) {
      const that = this
      that.$refs[form].validate((valid) => {
        if (valid) {
          updateMobile(this.form).then(response => {
            if (response.result === 1) {
              that.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$router.push('/console')
            } else {
              that.$message({
                message: '修改失败',
                type: 'warning'
              })
            }
          })
        } else {
          return false
        }
      })
    },
    onCancel() {
      const that = this
      that.$router.push({ path: that.redirect || '/console' })
    }
  }
}
</script>

<style >
.changePassword-background{
  min-height: 100%;
  overflow: hidden;
}
.changePassword{
  width: 450px;
  margin: 60px auto 0 auto;
}
.changePassword-title{
  height: 60px;
  text-align: center;
  font-size: 26px;
  font-weight: bold;
}
.code2{
  position:absolute;
  right: 0;
  top: 0;
  height: 32px;

}
</style>
